<template>
  <div class="mod-config">
    <el-form>
      <el-form-item>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('base:baseproductaudit:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button size="mini" v-if="isAuth('base:baseproductaudit:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">删除</el-button>
        </el-button-group>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('base:baseproductaudit:export')" type="primary" @click="exportData">导出</el-button>
          <el-button size="mini" v-if="isAuth('base:baseproductaudit:import')" type="primary" @click="importData()">导入</el-button>
         </el-button-group>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" size="mini" placeholder="参数名" clearable @clear="getDataList()" @blur="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList()">查询</el-button>
        <el-button @click="resetForm" size="mini" type="info">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      @cell-click="tableDbEdit"
      @cell-dblclick="tableDbEdit"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column type="index" header-align="center" align="center" label="#" width="40">
      </el-table-column>
        <el-table-column
        prop="auditId"
        header-align="center"
        align="center"
        label="">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.auditId" v-if="showInput == `auditId${scope.row.Id}`"
                      @blur='blurInput(scope.row, "auditId", scope.row.auditId)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.auditId}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="productId"
        header-align="center"
        align="center"
        label="产品ID">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.productId" v-if="showInput == `productId${scope.row.Id}`"
                      @blur='blurInput(scope.row, "productId", scope.row.productId)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.productId}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="purchaseAuditState"
        header-align="center"
        align="center"
        label="采购审核状态 0未审核， 1审核通过， -1审核不通过">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.purchaseAuditState" v-if="showInput == `purchaseAuditState${scope.row.Id}`"
                      @blur='blurInput(scope.row, "purchaseAuditState", scope.row.purchaseAuditState)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.purchaseAuditState}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="purchaseAuditUserId"
        header-align="center"
        align="center"
        label="采购人id">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.purchaseAuditUserId" v-if="showInput == `purchaseAuditUserId${scope.row.Id}`"
                      @blur='blurInput(scope.row, "purchaseAuditUserId", scope.row.purchaseAuditUserId)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.purchaseAuditUserId}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="purchaseAuditUserName"
        header-align="center"
        align="center"
        label="采购员名称">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.purchaseAuditUserName" v-if="showInput == `purchaseAuditUserName${scope.row.Id}`"
                      @blur='blurInput(scope.row, "purchaseAuditUserName", scope.row.purchaseAuditUserName)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.purchaseAuditUserName}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="purchaseAuditDate"
        header-align="center"
        align="center"
        label="采购审核日期">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.purchaseAuditDate" v-if="showInput == `purchaseAuditDate${scope.row.Id}`"
                      @blur='blurInput(scope.row, "purchaseAuditDate", scope.row.purchaseAuditDate)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.purchaseAuditDate}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="purchaseAuditRemark"
        header-align="center"
        align="center"
        label="采购审核说明">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.purchaseAuditRemark" v-if="showInput == `purchaseAuditRemark${scope.row.Id}`"
                      @blur='blurInput(scope.row, "purchaseAuditRemark", scope.row.purchaseAuditRemark)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.purchaseAuditRemark}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qcAuditState"
        header-align="center"
        align="center"
        label="质管审核状态 0未审核， 1审核通过， -1审核不通过">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qcAuditState" v-if="showInput == `qcAuditState${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qcAuditState", scope.row.qcAuditState)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qcAuditState}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qcAuditUserId"
        header-align="center"
        align="center"
        label="质管人id">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qcAuditUserId" v-if="showInput == `qcAuditUserId${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qcAuditUserId", scope.row.qcAuditUserId)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qcAuditUserId}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qcAuditUserName"
        header-align="center"
        align="center"
        label="质管员名称">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qcAuditUserName" v-if="showInput == `qcAuditUserName${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qcAuditUserName", scope.row.qcAuditUserName)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qcAuditUserName}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qcAuditDate"
        header-align="center"
        align="center"
        label="质管审核日期">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qcAuditDate" v-if="showInput == `qcAuditDate${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qcAuditDate", scope.row.qcAuditDate)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qcAuditDate}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qcAuditRemark"
        header-align="center"
        align="center"
        label="质管审核说明">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qcAuditRemark" v-if="showInput == `qcAuditRemark${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qcAuditRemark", scope.row.qcAuditRemark)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qcAuditRemark}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qaAuditState"
        header-align="center"
        align="center"
        label="质保审核状态 0未审核， 1审核通过， -1审核不通过">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qaAuditState" v-if="showInput == `qaAuditState${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qaAuditState", scope.row.qaAuditState)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qaAuditState}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qaAuditUserId"
        header-align="center"
        align="center"
        label="质保人id">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qaAuditUserId" v-if="showInput == `qaAuditUserId${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qaAuditUserId", scope.row.qaAuditUserId)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qaAuditUserId}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qaAuditUserName"
        header-align="center"
        align="center"
        label="质保员名称">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qaAuditUserName" v-if="showInput == `qaAuditUserName${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qaAuditUserName", scope.row.qaAuditUserName)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qaAuditUserName}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qaAuditDate"
        header-align="center"
        align="center"
        label="质保审核日期">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qaAuditDate" v-if="showInput == `qaAuditDate${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qaAuditDate", scope.row.qaAuditDate)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qaAuditDate}}</span>
                  </template>
      </el-table-column>
          <el-table-column
        prop="qaAuditRemark"
        header-align="center"
        align="center"
        label="质保审核说明">
        <template slot-scope="scope">
                      <el-input size="mini" v-model="scope.row.qaAuditRemark" v-if="showInput == `qaAuditRemark${scope.row.Id}`"
                      @blur='blurInput(scope.row, "qaAuditRemark", scope.row.qaAuditRemark)' v-focus>
            </el-input>
            <span v-else style="text-decoration:none;">{{scope.row.qaAuditRemark}}</span>
                  </template>
      </el-table-column>
        <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="isAuth('base:baseproductaudit:save')" @click="saveHandle(scope.row)">保存</el-button>
          <el-button size="mini" type="text" v-if="isAuth('base:baseproductaudit:update')" @click="addOrUpdateHandle(scope.row.auditId)">修改</el-button>
          <el-button size="mini" type="text" v-if="isAuth('base:baseproductaudit:delete')" @click="deleteHandle(scope.row.auditId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './productaudit-add-or-update'
  import {downLoadFile} from '@/utils/myUtil.js'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        showInput: "",
        oldData: {},
        currentData: {},
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async getDataList () {
        this.dataListLoading = true
        const res = await this.$http({
          url: `/product/audit/list`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': this.dataForm
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.dataList.forEach(item => {
            item.status = item.status == 1 ? true : false
          })
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.auditId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {
          const res = await this.$http({
            url: `/product/audit/delete`,
            method: 'post',
            data: this.$http.adornData(ids, false)
          })
          let data = res.data
          if (data && data.code === 0) {
            this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
              this.getDataList()
            }
          })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      resetForm(){
        this.$nextTick(() =>{
          this.dataForm = this.$options.data().dataForm
          this.getDataList()
        })
      },
      async saveHandle(row){
        row.status = row.status ? 1 : 0
        const res = await this.$http({
          url:`/product/audit/update`,
          method: 'post',
          data: this.$http.adornData(row)
        });
        let data = res.data
        if (data && data.code !== 0) {
          row.status = !row.status;
          return this.$message.error('修改失败!');
        }
        this.$message.success('更新成功！');
        this.getDataList();
      },
      // 当input失去光标后进行的操作
      async blurInput(row, name, value) {
        // 判断数据是否有所改变，如果数据有改变则调用修改接口
        if (this.oldData[name] != value) {
          row[name] = value
          if(value === true){
            row[name] = 1
          }else if(value === false){
            row[name] = 0
          }
        }
        this.showInput = ""
        this.currentData = row
      },
      tableDbEdit(row, column, event) {
        this.showInput = column.property + row.auditId;
        this.oldData[column.property] = row[column.property];
      },
      async exportData(){
        const res = await this.$http({
          url:`/product/audit/exportData`,
          method: 'get',
          responseType: 'blob',
          params: this.$http.adornParams()
        });
        downLoadFile(res)
      }
    },
    directives: {
      // 通过自定义指令实现的表单自动获得光标的操作
      focus: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "input") {
            el.focus()
          } else {
            if (el.getElementsByTagName("input")) {
              el.getElementsByTagName("input")[0].focus()
            }
          }
          el.focus()
        }
      },
      focusTextarea: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "textarea") {
            el.focus()
          } else {
            if (el.getElementsByTagName("textarea")) {
              el.getElementsByTagName("textarea")[0].focus()
            }
          }
          el.focus()
        }
      }
    },
  }
</script>
